package com.bishe.demo.modules.hotel.hotelMain.service.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.hotel.hotelDetail.serivce.DetailService;
import com.bishe.demo.modules.hotel.hotelFacilityAndService.entity.FacilityAndService;
import com.bishe.demo.modules.hotel.hotelFacilityAndService.service.FacilityAndServiceService;
import com.bishe.demo.modules.hotel.hotelMain.dao.HotelMainDao;
import com.bishe.demo.modules.hotel.hotelMain.dao.HotelMainUserDao;
import com.bishe.demo.modules.hotel.hotelMain.entity.HotelMain;
import com.bishe.demo.modules.hotel.hotelMain.entity.HotelMainUser;
import com.bishe.demo.modules.hotel.hotelMain.service.HotelMainService;
import com.bishe.demo.modules.hotel.hotelPolicy.service.PolicyService;
import com.bishe.demo.modules.hotel.hotelRoom.service.RoomService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class HotelMainServiceImpl implements HotelMainService {
    @Autowired
    private HotelMainDao hotelMainDao;

    @Autowired
    private HotelMainUserDao hotelMainUserDao;

    @Autowired
    private FacilityAndServiceService facilityAndServiceService;

    @Autowired
    private DetailService detailService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private PolicyService policyService;

    @Override
    @Transactional
    public ResultEntity<HotelMain> insertHotelMain(HotelMain hotelMain) {
        HotelMain hotelMainByUserId = hotelMainDao.getHotelMainByUserId(hotelMain.getUser().getUserId());
        if (hotelMainByUserId == null) {
            hotelMain.setHotelMainId(hotelMain.getUser().getUserId());
            hotelMainDao.insertHotelMain(hotelMain);
            hotelMainUserDao.insertHotelMainUser(new HotelMainUser(hotelMain.getHotelMainId(), hotelMain.getUser().getUserId()));
            return new ResultEntity<HotelMain>(ResultEntity.ResultStatus.SUCCESS.code, "添加成功", hotelMain);
        } else {
            return new ResultEntity<HotelMain>(ResultEntity.ResultStatus.FAILED.code, "已有酒店基本信息");
        }
    }

    @Override
    public List<HotelMain> getHotelMains() {
        return hotelMainDao.getHotelMains();
    }

    @Override
    public List<HotelMain> getHotelMainsByProvince(int province) {
        return hotelMainDao.getHotelMainsByProvince(province);
    }

    @Override
    public List<HotelMain> getHotelMainsByCity(int city) {
        return hotelMainDao.getHotelMainsByCity(city);
    }

    @Override
    public List<HotelMain> getHotelMainByArea(int area) {
        return hotelMainDao.getHotelMainByArea(area);
    }

    @Override
    public HotelMain getHotelMainByHotelId(int hotelId) {
        HotelMain hotelMainByHotelId = hotelMainDao.getHotelMainByHotelId(hotelId);
        FacilityAndService facilityAndServiceByHotelId = facilityAndServiceService.getFacilityAndServiceByHotelId(hotelId);
        hotelMainByHotelId.setFacilityAndService(facilityAndServiceByHotelId);
        return hotelMainByHotelId;
    }

    @Override
    public HotelMain getHotelMainById(int userId) {
        return hotelMainDao.getHotelMainByUserId(userId);
    }

    @Override
    public ResultEntity<HotelMain> updateHotelMain(HotelMain hotelMain) {
        hotelMainDao.updateHotelMain(hotelMain);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.code, "修改成功", hotelMain);
    }

    @Override
    @Transactional
    public ResultEntity<HotelMain> deleteHotelMain(int hotelId) {
        HotelMain hotelMainByHotelId = hotelMainDao.getHotelMainByHotelId(hotelId);
        hotelMainDao.deleteHotelMainByHotelId(hotelId);
        hotelMainUserDao.deleteByHotelId(hotelId);
        detailService.deleteDetailByHotelId(hotelId);
        roomService.delete(hotelMainByHotelId.getHotelRooms());
        facilityAndServiceService.deleteByHotelId(hotelId);
        policyService.deletePolicy(hotelMainByHotelId.getPolicy());
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.code, "删除成功");
    }

    @Override
    public PageInfo<HotelMain> getHotelsBySearchBeans(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<HotelMain>(Optional
                .ofNullable(hotelMainDao.getHotelsBySearchBeans(searchBean))
                .orElse(Collections.emptyList()));
    }

    @Override
    public List<HotelMain> getHotelMainsForMain() {
        return hotelMainDao.getHotelMainsForMain();
    }

}
